<template>
	<view class="container">
		<view class="query-form">
			<view class="query-time">
				<uni-datetime-picker type="daterange" :clear-icon="true" v-model="dateRange" />
			</view>
			<view class="query-item">
				<uni-data-select v-model="queryParams.stadiumId" :localdata="stadiumRange"
					placeholder="请选择场馆"></uni-data-select>
			</view>
			<view class="query-item">
				<uni-data-select v-model="queryParams.classesId" :localdata="classesRange"
					placeholder="请选择班次"></uni-data-select>
			</view>
			<view class="query-item">
				<button class="uni-button" size="mini" type="primary" @click="handleQuery">搜索</button>
			</view>
		</view>
		<uni-section :title="'总学员人数:共 ' + totalStudent + ' 人, 共 ' + total + ' 课时'" type="line"></uni-section>
		<uni-table ref="table" :loading="loading" border stripe emptyText="暂无更多数据">
			<uni-tr>
				<uni-th align="center">课时信息</uni-th>
				<uni-th align="center">学员信息</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in lessonList" :key="index">
				<uni-td>
					<view class="name">{{ parseTime(item.createTime, '{y}-{m}-{d}') }}</view>
					<view class="name">{{ getCoachName(item.coachId) }}</view>
					<view class="name">{{ getStadiumName(item.stadiumId) }}</view>
					<view class="name">{{ getClassesName(item.classesId) }}</view>
					<view class="name remark">{{ item.remark }}</view>
				</uni-td>
				<uni-td align="center">
					<view class="name">{{ item.puyuLessonStudents.length }}个学生：</view>
					<view class="name">{{ getStudentNames(item.puyuLessonStudents) }}</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<uni-pagination :page-size="queryParams.pageSize" :current="queryParams.pageNum" :total="total" @change="change"
			prev-text="前一页" next-text="后一页" />
		<view class="page-sum">
			<text>当前页：{{ queryParams.pageNum }}，数据总量：{{ total }} 条，每页数据：{{ queryParams.pageSize }} 条</text>
		</view>
	</view>
</template>

<script>
	import {
		checkPermi,
		checkRole
	} from "@/utils/permission";
	import {
		listLesson,
		getLesson,
		delLesson,
		addLesson,
		updateLesson
	} from "@/api/puyu/lesson";
	import {
		listAllCoachs
	} from "@/api/puyu/coach";
	import {
		getDicts
	} from "@/api/system/dict/data";
	import { parseTime } from '@/utils/common';
	let defaultDate = parseTime(new Date(), '{y}-{m}-{d}');
	let defaultDateRange = [defaultDate, defaultDate];
	export default {
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总学员人数
				totalStudent: 0,
				// 总条数
				total: 0,
				// 课时表格数据
				lessonList: [],
				// 教练数据
				coachList: [],
				// 日期范围
				dateRange: defaultDateRange,
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 5,
					deptId: null,
					coachId: null,
					stadiumId: null,
					classesId: null,
					createTime: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				puyuClassesOptions: [],
				puyuStadiumOptions: [],
			}
		},
		computed: {
			stadiumRange() {
				if (!this.puyuStadiumOptions) {
					return [];
				}
				let arr = [];
				this.puyuStadiumOptions.map(item => {
					let obj = {
						text: item.dictLabel,
						value: item.dictValue,
					}
					arr.push(obj);
				});
				return arr;
			},
			classesRange() {
				if (!this.puyuClassesOptions) {
					return [];
				}
				let arr = [];
				this.puyuClassesOptions.map(item => {
					let obj = {
						text: item.dictLabel,
						value: item.dictValue,
					}
					arr.push(obj);
				});
				return arr;
			},
		},
		created() {
			let d = this.formatDate(new Date())
			this.dateRange = [d,d]
			this.getList();
			this.getCoachList();
		},
		onLoad(option) {
			this.queryParams.coachId = option.coachId;
			// this.selectedIndexs = []
			// this.getData(1)
		},
		onShow() {
			this.getList();
		},
		methods: {
			checkPermi,
			checkRole,
			parseTime,
			// 格式化日期为YYYY-MM-DD
			formatDate(date) {
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				return `${year}-${month}-${day}`;
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 查询课时列表 */
			getList() {
				this.loading = true;
				this.queryParams.orderByColumn = 'lessonId'; //查询字段是表格中字段名字
				this.queryParams.isAsc = 'descending'; //动态取值排序顺序
				listLesson(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
					this.lessonList = response.rows;
					this.total = response.total;
					this.getStudentTotal(this.lessonList)
					this.loading = false;
				});
			},
			/** 查询教练用户列表 */
			getCoachList() {
				listAllCoachs().then(response => {
					this.coachList = response.data;
				});
				getDicts("puyu_classes").then(response => {
					this.puyuClassesOptions = response.data;
				});
				getDicts("puyu_stadium").then(response => {
					this.puyuStadiumOptions = response.data;
				});
			},
			handleCoachChange(coach) {
				this.form.userId = coach.userId;
				this.form.deptId = coach.deptId;
			},
			// 根据coachId获取教练姓名
			getCoachName(coachId) {
				if (this.coachList) {
					return this.coachList.filter(item => item.coachId == coachId)[0].coachName;
				}
				return '';
			},
			// 根据classesId获取班次
			getClassesName(classesId) {
				if (this.puyuClassesOptions) {
					return this.puyuClassesOptions.filter(item => item.dictValue == classesId)[0].dictLabel;
				}
				return '';
			},
			// 根据stadiumId获取场馆
			getStadiumName(stadiumId) {
				if (this.puyuStadiumOptions) {
					return this.puyuStadiumOptions.filter(item => item.dictValue == stadiumId)[0].dictLabel;
				}
				return '';
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					lessonId: null,
					userId: null,
					deptId: null,
					coachId: null,
					stadiumId: null,
					classesId: null,
					delFlag: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					remark: null
				};
				this.resetForm("form");
			},
			// 分页触发
			change(e) {
				this.$refs.table.clearSelection()
				this.ids.length = 0
				this.getData(e.current)
			},
			// 获取数据
			getData(pageCurrent, value = '') {
				this.queryParams.pageNum = pageCurrent
				this.getList()
			},
			/** 拼接学员姓名 */
			getStudentNames(puyuLessonStudents) {
				if (puyuLessonStudents.length == 0) {
					return '';
				}
				let names = '';
				for(let i = 0; i < puyuLessonStudents.length; i++) {
					if (i === 0) {
						names = puyuLessonStudents[i].studentName;
					} else {
						names = names + ',' + puyuLessonStudents[i].studentName;
					}
				}
				return names;
			},
			/** 统计总学员人数 */
			getStudentTotal(list) {
				if (list.length == 0) {
					return 0;
				}
				let total = 0;
				for(let i = 0; i < list.length; i++) {
					total = total + list[i].puyuLessonStudents.length;
				}
				this.totalStudent = total;
			},
		}
	}
</script>

<style scoped>
	/* #ifndef H5 */
	/* page {
	padding-top: 85px;
} */
	/* #endif */
	.uni-group {
		display: flex;
		align-items: center;
	}

	.btn-rollcall {
		color: #ffffff;
		backgroundColor: #1AAD19;
		borderColor: #1AAD19
	}

	.page-sum {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.query-form {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		background-color: #ffffff;
		margin-bottom: 10px;
	}

	.query-item {
		margin: 5rpx;
		padding: 5rpx;
		width: 180rpx;
	}

	.query-time {
		margin: 5rpx;
		padding: 5rpx;
		width: 80%;
	}
	
	.remark {
		color: #993399;
		font-weight: bolder;
	}
</style>